package org.example.chapter1;

/**
 * 伪共享
 */
public class S211_FalseSharing {

    static final int LINE_NUM = 1024;
    static final int COLUM_NUM = 1024;

    public static void main(String[] args) {
        method1();// cache time:5
        method2();// no cache time:4
    }

    private static void method1() {
        long [][] array = new long[LINE_NUM][COLUM_NUM];
        long startTime = System.currentTimeMillis();
        for(int i =0; i<LINE_NUM; ++i){
            for(int j=0; j<COLUM_NUM; ++j){
                array[i][j] = i * 2 + j;
            }
        }
        long endTime = System.currentTimeMillis();
        long cacheTime = endTime - startTime;
        System.out.println("cache time:" + cacheTime);
    }

    public static void method2() {
        long [][] array = new long[LINE_NUM][COLUM_NUM];
        long startTime = System.currentTimeMillis();
        for(int i =0; i<COLUM_NUM; ++i){
            for(int j=0; j<LINE_NUM; ++j){
                array[j][i] = i * 2 + j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("no cache time:" + (endTime - startTime));
    }
}
